<?php


namespace app\service;


class CrawlerService
{

    public static function start($data)
    {

        $url = $data['url'];
        $urlInfo = parse_url($url);

        $resultPath = "./tmp/{$urlInfo['host']}";
        if (file_exists($resultPath) == false) {
            exec("mkdir -p $resultPath");
        }


        $resultFileName = "{$resultPath}/" . date('Y-m-d-H-i-s') . ".json";
        $cmd = "./tools/rad_linux_amd64 -t {$url}  --json  $resultFileName";


        exec($cmd);

        self::SendCenter($data['callUrl'], $data['jid'], $resultFileName);
    }

    /**
     * 发送爬虫结果到中心
     * @param string $callUrl
     * @param int $jid
     * @param string $resultFileName
     */
    public static function SendCenter(string $callUrl, int $jid, string $resultFileName)
    {
        //读取URL地址
        $data = json_decode(file_get_contents($resultFileName), true);

        //组装数据
        foreach ($data as &$value) {
            $value['method'] = $value['Method'];
            $value['url'] = $value['URL'];
            $value['jid'] = $jid;
            unset($value['Method']);
            unset($value['Header']);
            unset($value['URL']);
        }

        //开始发送
        $data = json_encode($data);
        $result = http_request_code($callUrl, null, 'POST', ['data' => $data], 50);

        addlog(["发送爬虫结果", $result]);
    }
}